package com.example.gamestore.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gamestore.Bean.Msg;
import com.example.gamestore.Bean.User;
import com.example.gamestore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.sql.Wrapper;

@Controller
public class UserControl {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public String login(Model model, User user, HttpSession Session) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername())
                .eq("password", user.getPassword());
        User user1 = userService.getOne(wrapper);
        if (user1 != null) {
            if (user1.getStatus() == 1) {
                Session.setAttribute("user", user1);
                return "redirect:/main";
            } else {
                model.addAttribute("error", "此用户已经被封禁");
                return "index";
            }
        } else {
            model.addAttribute("error", "密码错误");
            return "index";
        }
    }

    @GetMapping("/main")
    public String loginsuccess() {
        return "index1";
    }

    @GetMapping("/registersuccess")
    public String registersuccess() {
        return "index";
    }

    @PostMapping("/register")
    public String register(User user, Model model) {
        String username = user.getUsername();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username).last("limit 1");
        User user1 = userService.getOne(wrapper);
        if (user1 != null) {
            model.addAttribute("error", "用户名已经存在");
            return "register";
        }
        user.setAccount(2000);
        user.setStatus(1);
        userService.save(user);
        model.addAttribute("error", "注册成功");
        return "redirect:/registersuccess";
    }

    @PostMapping("/checkusername")
    @ResponseBody
    public Msg checkusername(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username).last("limit 1");
        Msg msg = new Msg();
        User user1 = userService.getOne(wrapper);
        if (user1 != null) {
            msg.setError("用户名已经存在");
            return msg;
        }
        msg.setRight("此用户名可以使用");
        return msg;

    }

    @GetMapping("/logout")
    public String loginout(HttpSession session) {
        session.removeAttribute("user");
        return "index";
    }

    @GetMapping("rlogin")
    public String rlogin() {
        return "index";
    }

    @PostMapping("/chongzhi")
    @ResponseBody
    public String chongzhi(String money){
            return "充值"+money;
    }
}
